package net.imagine.backend.mockrmi.local;

import net.imagine.backend.mockrmi.ProxyLookuper;
import net.imagine.backend.mockrmi.ServiceRegistry;
import net.imagine.backend.mockrmi.util.MockRmiUtils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 本地的ProxyLookuper实现，不需要通过TCP/IP，单机时提示效率
 * 
 * @see ProxyLookuper
 * @author wuqh
 * @version 1.01
 */
public class LocalProxyLookuper implements ProxyLookuper {
	private static final Log logger = LogFactory.getLog(LocalProxyLookuper.class);

	@Override
	public Object lookup(String uri) {
		return lookup(uri, Object.class);
	}

	@Override
	public Object lookup(String domain, int port, String id) {
		return lookup(domain, port, id, Object.class);
	}

	@Override
	public <T> T lookup(String uri, Class<T> clazz) {
		String[] result = MockRmiUtils.parseUri(uri);
		int port = MockRmiUtils.PORT;
		try {
			port = Integer.parseInt(result[1]);
		} catch (NumberFormatException e) {
			logger.debug("port not numeric,use default port" + MockRmiUtils.PORT, e);
		}
		return lookup(result[0], port, result[2], clazz);
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T lookup(String domain, int port, String id, Class<T> clazz) {
		logger.debug("in local never use domain and port, just use id");
		return (T) ServiceRegistry.lookup(id);
	}

}
